package io.renren.modules.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.user.entity.TPeopleChangeDetailsEntity;
import io.renren.modules.user.entity.TPeopleChangeEntity;
import io.renren.modules.user.entity.TPeopleEntity;
import io.renren.modules.user.entity.form.PeopleChangeForm;
import io.renren.modules.user.entity.form.PeopleEntityForm;
import io.renren.modules.user.entity.form.PeopleForm;
import io.renren.modules.user.service.TPeopleChangeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 人员变更
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-02 10:30:00
 */
@RestController
@RequestMapping("/tpeoplechange")
public class TPeopleChangeController extends AbstractController {
    @Autowired
    private TPeopleChangeService tPeopleChangeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tpeoplechange:tpeoplechange:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tPeopleChangeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tpeoplechange:tpeoplechange:info")
    public R info(@PathVariable("id") Integer id){
		TPeopleChangeEntity tPeopleChange = tPeopleChangeService.getById(id);

        return R.ok().put("tPeopleChange", tPeopleChange);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tpeoplechange:tpeoplechange:save")
    public R save(@RequestBody TPeopleChangeEntity tPeopleChange){
		tPeopleChangeService.save(tPeopleChange);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
 //   @RequiresPermissions("generator:tpeoplechange:update")
    public R update(@RequestBody TPeopleChangeEntity tPeopleChange){
		tPeopleChangeService.updateById(tPeopleChange);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:tpeoplechange:delete")
    public R delete(@RequestBody Integer[] ids){
		tPeopleChangeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     *附件下载
     */
    @RequestMapping("attachmentDownload")
    //@RequiresPermissions("tpeoplechange:tpeoplechange:attachmentDownload")
    public R info(@RequestBody TPeopleChangeEntity tPeopleChange){
        List<TPeopleChangeEntity> list=tPeopleChangeService.attachmentDownload(tPeopleChange);
        return R.ok().put("list", list);
    }


     /*
     *
     * 人员调动
     * */
     @RequestMapping("/updatepeopleChange")
     //@RequiresPermissions("tpeople:tpeoplechange:change")
     public R updatepeopleChange(@RequestBody TPeopleChangeEntity tPeopleChangeEntity){
         tPeopleChangeEntity.setCreateUser(getUserId().intValue());

         tPeopleChangeService.updatepeopleChange(tPeopleChangeEntity);
         return R.ok();
     }


     /*
     * 人员变更信息查询
     * */
     @RequestMapping("/findPeopleChange")
     @RequiresPermissions("tpeoplechange:tpeoplechange:select")
     public R findPeopleChange(@RequestBody PeopleForm peopleEntityVo){
         String sql = this.doSql(peopleEntityVo.getPeopleEntityForms(),peopleEntityVo.getFlag());
         Map<String,Object> params=new HashMap<String,Object> ();
         String sqls=null;
         if(!sql.equals("")){
             String a=sql.substring(0,3);
             if("and".equals(a)){
                 sqls = deleteAssignChar(sql, 0, 'a');
                 String sa = deleteAssignChar(sqls, 1, 'a');
                 String sb = deleteAssignChar(sa, 1, 'n');
                 sqls = deleteAssignChar(sb, 1, 'd');
             }
             if("or ".equals(a)){
                 sqls = deleteAssignChar(sql, 0, 'o');
                 String sz = deleteAssignChar(sqls, 1, 'o');
                 sqls = deleteAssignChar(sz, 1, 'r');
             }

             System.out.print(sqls);
         }
         params.put("sqls",sqls);
         params.put("page",peopleEntityVo.getPage());
         params.put("limit",peopleEntityVo.getLimit());
         params.put("detachment",peopleEntityVo.getDetachment());
         params.put("squadron",peopleEntityVo.getSquadron());
         PageUtils page = tPeopleChangeService.findPeopleChange(params);
         return R.ok().put("page",page);
     }


    public static String deleteAssignChar(String str, int on, char c){
        int count = 0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i) == c){
                count++;
                if(count == on){
                    str = str.substring(0, i) + str.substring(i + 1);
                }
            }
        }
        return str;
    }


    /*
     * 拼接sql
     * */
    private String doSql(List<PeopleEntityForm> tPeopleEntityList, String flag){
        StringBuffer sqlString=new StringBuffer();
        if(tPeopleEntityList !=null){
            if("0".equals(flag)) {
                for (PeopleEntityForm tPeopleForm : tPeopleEntityList) {
                    if ("pname".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and p.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and p.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("newDetachment".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and d.detachment " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and d.detachment " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("oldDetachment".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and pd.detachment " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and pd.detachment " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("newName".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and ttd.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and ttd.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("oldName".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and td.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and td.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("changeTime".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and b.change_time " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and b.change_time " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("name".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and a.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and a.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("changType".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("and b.change_type " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("and b.change_type " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }

                }
            } else{
                for (PeopleEntityForm tPeopleForm : tPeopleEntityList) {
                    if ("pname".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or p.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or p.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("newDetachment".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or d.detachment " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or d.detachment " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("oldDetachment".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or pd.detachment " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or pd.detachment " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("newName".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or ttd.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or ttd.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("oldName".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or td.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or td.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("changeTime".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or b.change_time " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or b.change_time " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("name".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or a.name " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or a.name " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                    if ("changeType".equals(tPeopleForm.getFour())) {
                        if("like".equals(tPeopleForm.getTwo()) || "not like".equals(tPeopleForm.getTwo())){
                            sqlString.append("or b.change_type " + tPeopleForm.getTwo() +"'"+'%'+tPeopleForm.getThree()+'%'+"'");
                        }else {
                            sqlString.append("or b.change_type " + tPeopleForm.getTwo() + "'" + tPeopleForm.getThree() + "'");
                        }
                    }
                }
            }
        }


        return sqlString.toString();
    }




}
